﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using COFE.Core;
using System.Xml.Linq;

namespace COFE.Web
{
    public class EventList : SupportLink, ISupportXml, ISupportLinkTemplates
    {
        #region Constants

        public static string ParseDateTimeFormat = "yyyyMMdd";

        #endregion

        #region Constructor

        protected EventList(DateTime utcDate)
        {
            _eventDateUtc = utcDate;
            DateTime startTimeUtc = new DateTime(utcDate.Year, utcDate.Month, utcDate.Day, 0, 0, 0);
            DateTime endTimeUtc = new DateTime(utcDate.Year, utcDate.Month, utcDate.Day, 23, 59, 59);
            _hasPrevEvents = COFEShared.MasterEventLogger.GetEventLog(
                new DateTime(2012, 1, 1), startTimeUtc).Any();
            _events = from e in
                          COFEShared.MasterEventLogger.GetEventLog(startTimeUtc, endTimeUtc)
                      select Event.FromEventLog(e);
        }

        public static EventList ForDay(DateTime utcDate)
        {
            return new EventList(utcDate);
        }

        public static EventList ForToday()
        {
            return EventList.ForDay(DateTime.Today);
        }

        #endregion

        #region Methods

        public override IEnumerable<Link> GetLinks()
        {
            return this.CreateLinks();
        }

        public IEnumerable<LinkTemplate> GetLinkTemplates()
        {
            yield return new LinkTemplate(MediaType.EventList,
                RelationType.Self, ServiceConstants.Templates.EventList);
            if (DateTime.Now.Date > _eventDateUtc.Date)
                yield return new LinkTemplate(MediaType.EventList,
                    RelationType.Next, ServiceConstants.Templates.NextEventList);
            if (_hasPrevEvents)
                yield return new LinkTemplate(MediaType.EventList,
                    RelationType.Prev, ServiceConstants.Templates.PrevEventList);
        }

        public Dictionary<string, string> GetLinkParameterDic()
        {
            return new Dictionary<string, string>()
            {
                { "#id#", this._eventDateUtc.ToString(ParseDateTimeFormat) },
                { "#nextid#", this._eventDateUtc.AddDays(1).ToString(ParseDateTimeFormat) },
                { "#previd#", this._eventDateUtc.AddDays(-1).ToString(ParseDateTimeFormat) }                
            };
        }

        public XElement ToXml(bool indentLinks = false)
        {
            var eventList = new XElement(XNames.List,
                                 new XAttribute(XNames.Type, "Event")
                                 );

            var events = eventList;
            if (indentLinks)
            {
                events = new XElement(XNames.Events);
                eventList.Add(events);
            }

            foreach (var e in Events)
                events.Add(e.ToXml(indentLinks));

            writeLinks(eventList, indentLinks);

            return eventList;
        }

        #endregion

        #region Data
        bool _hasPrevEvents;
        IEnumerable<Event> _events;
        List<Link> _links = new List<Link>();
        DateTime _eventDateUtc;
        #endregion

        #region Public Properties

        public IEnumerable<Event> Events { get { return _events; } }

        /// <summary>
        /// Gets a list of links.
        /// </summary>
        public List<Link> Links { get { return _links; } private set { _links = value; } }

        #endregion



    }
}
